<?php
class memberApi extends core 
{
	public function __construct()
	{
	}
	
	public function loginAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');

		$template	= $this->template();
		
		if (base::init('member')->isLogin())
		{
			$columns	= base::init('column')->fetchAll("wid={$arg['wid']}");
			$template->assign('columns',$columns);
			$template->assign('referrer','/');
			$template->assign('stat','您已经登录了~');
			$template->display('refresh.php');
			die;
		}

		if (isset($arg['username']) && isset($arg['password']))
		{
			if (($userinfo	= base::init('member')->login($arg)) !== FALSE)
			{
				$hash	= base::init('member')->cookiehash($arg['username'], get_real_ip(), $_SERVER['HTTP_USER_AGENT']);

				if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');
				
				$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
				$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
				$columns	= base::init('column')->listcls($columns,0);
				
				
				
				$template->assign('columns',$columns);
				$template->assign('referrer',$arg['referrer']);
				$template->assign('stat','登录成功~');
				
				
				setcookie("user",$hash,time()+3600*24,"/");
				setcookie("username",$arg['username'],time()+3600*24,"/");
				setcookie("uid",$userinfo['id'],time()+3600*24,"/");
				setcookie("wid",$userinfo['wid'],time()+3600*24,"/");
				
				$template->display('refresh.php');
				die;
			}
			else 
			{
				$message	= 'A record with the supplied identity could not be found or Supplied credential is invalid <br />用户名或密码无效';
				$template->assign('message', $message);
			}
		}	

		
		$template->assign('url', @$arg['referer']);
		$template->display('member_login.php');
		die;
	}

	public function registerAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');
		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
		$columns	= base::init('column')->listcls($columns,0);
		
		$city		= base::init('city')->fetchAll();
		$province	= base::init('province')->fetchAll();
		//$city	= sort_combine($city, "ProvinceID");

		$template	= $this->template();
		$template->template_dir = ROOT . $attr['tempdir'] ;
		$template->assign('attr',$attr);
		$template->assign('city',($city));
		$template->assign('province',$province);
		$template->assign('columns',$columns);

		if (isPost())
		{
			include ROOT . '/libs/Zend/Session/Namespace.php';
			$codeSession	= new Zend_Session_Namespace('code');
			
			if (empty($arg['code']) || $arg['code'] !== $codeSession->code)
			{
				$template->assign('url','/app/?m=member&a=register');
				$template->assign('stat','验证码错误~');
				$template->display('refresh.php');
				die;
			}
			
			if (!base::init('member')->checkUsername($arg['username']))
			{
				$template->assign('url','/app/?m=member&a=register');
				$template->assign('stat','用户名已经被使用了~');
				$template->display('refresh.php');
				die;
			}
			else 
			{
				$arg_					= $arg;
				$arg_['register_time']	= time();
				$arg_['password']		= md5($arg['password']);
				$arg_['login_time']		= time();
				
				//加入用户表
				$member_id	= base::init('member')->insert($arg_);
				//加入用户组
				//默认用户组
				$group		= base::init('member_group')->fetchRow("def=1");
				$group_id	= $group['id'];
				base::init('member_group_control')->insert(
					array('member_id'=>$member_id,'member_group_id'=>$group_id)
				);
				
				$template->assign('referrer',$arg['referrer']);
				$template->assign('stat','注册成功');

				$userinfo	= base::init('member')->login($arg);
				$hash	= base::init('member')->cookiehash($arg['username'], get_real_ip(), $_SERVER['HTTP_USER_AGENT']);

				setcookie("user",$hash,time()+3600*24,"/");
				setcookie("username","".$arg['username'],time()+3600*24,"/");
				setcookie("uid",$userinfo['id'],time()+3600*24,"/");
				setcookie("wid",$userinfo['wid'],time()+3600*24,"/");

				$template->display('refresh.php');
			}
		}
		else 
		{
			$template->display('member_register.php');
		}
	}
	
	public function menulistAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');

		$template	= $this->template();
		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);

		if (isset($_COOKIE['username']))
		{
			$islogin	= @base::init('member')->isLogin();
			$template->assign('islogin',$islogin);
			$template->assign('userinfo',$_COOKIE);
		}
		
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
		$classid	= array_get_unique($columns,'classid');
		$list		= base::init('classified')->fetchAll(array('classid'=>$classid,'audit'=>1),"id desc",10,0);
		$list		= base::init('classified')->getTempList($list,$columns);
		

		$template->assign('list',$list);
		$html	= $template->fetch(ROOT . $attr['tempdir'] . 'member_menulist.php');
		die(json_encode(array('html'=>$html)));
	}

	public function logoutAction($arg)
	{
		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
		$columns	= base::init('column')->listcls($columns,0);

		$template	= $this->template();
		$template->template_dir = ROOT . $attr['tempdir'] ;

		$template->assign('attr',$attr);
		$template->assign('columns',$columns);
		$template->assign('referrer',isset($arg['referrer'])?$arg['referrer']:'/'); 
		$template->assign('stat','退出成功~');

		setcookie("username","",time()-3600*24*365,"/");
		$template->display('refresh.php');
	}
	
	public function regcheckAction($arg)
	{
		if (isset($arg['username']))
		{
			$user	= base::init('member')->fetchRow(array('username'=>$arg['username']));
			if ($user)	die('0');
				else die('1');
		}
		else if (isset($arg['code']))
		{
			include ROOT . '/libs/Zend/Session/Namespace.php';
			$codeSession	= new Zend_Session_Namespace('code');
			
			if ($arg['code'] == $codeSession->code) die('1');
				else die('0');
		}
	}
	
	//个人空间
	public function homeAction($arg)
	{
		base::init('member')->needLogin();
		
		$template	= $this->template();

		//userinfo
		$userinfo	= base::init('member')->getInfo();
		//常见问题
		$column		= sort_combine(base::init('column')->fetchAll('classid=525','','','',false),'classid',true);	
		$faq		= base::init('article')->fetchAll('classid=525');
		$faq		= base::init('article')->getTempList($faq,$column);
		//企业信息
		$business	= base::init('business')->fetchRowByUid($userinfo['id']);
		
		$template->assign('faq', $faq);
		$template->assign('business', $business);
		$template->assign('userinfo', $userinfo);
		
		$template->display('member_home.php');
	}
	//修改个人信息
	public function editAction($arg)
	{
		base::init('member')->needLogin();
		
		$template	= $this->template();
		//userinfo
		$userinfo	= base::init('member')->getInfo();
		
		if (isPost())
		{
			base::init('member')->update($arg,"id={$userinfo['id']}");
			$template->assign('referrer','/app/?m=member&a=home'); 
			$template->assign('stat','修改成功');
			$template->display('refresh.php');
			die;
		}
		
		//常见问题
		$column		= sort_combine(base::init('column')->fetchAll('classid=525','','','',false),'classid',true);	
		$faq		= base::init('article')->fetchAll('classid=525');
		$faq		= base::init('article')->getTempList($faq,$column);
		
		$city		= base::init('city')->fetchAll();
		$province	= base::init('province')->fetchAll();
		
		$template->assign('faq', $faq);
		$template->assign('city', $city);
		$template->assign('province', $province);
		$template->assign('userinfo', $userinfo);
		$template->display('member_edit.php');
	}
	
	//修改密码
	public function repassAction($arg)
	{
		base::init('member')->needLogin();
		
		$userinfo	= base::init('member')->getInfo();
		$template	= $this->template();
		
		if (isPost())
		{
			$referrer	= '/app/?m=member&a=repass';
			if(md5($arg['o_password']) == $userinfo['password'])
			{
				if($arg['password'] == $arg['password1'])
				{
					base::init('member')->update(
						array('password'=>md5($arg['password'])),
						array('id'=>$userinfo['id'])
					);
					$referrer	= '/app/?m=member&a=home';
					$stat		= '修改成功！';
				}
				else 
				{
					$stat	= '两次密码输入不一致！';
				}
			}
			else 
			{
				$stat	= '旧密码输入错误！';
			}
			$template->assign('referrer',$referrer); 
			$template->assign('stat',$stat);
			$template->display('refresh.php');
		}
		else 
		{
			//常见问题
			$column		= sort_combine(base::init('column')->fetchAll('classid=525','','','',false),'classid',true);
			$faq		= base::init('article')->fetchAll('classid=525');
			$faq		= base::init('article')->getTempList($faq,$column);
			$template->assign('faq', $faq);
			
			$template->assign('userinfo', $userinfo);
			$template->display('member_repass.php');
		}
	}
	
	//信息列表
	public function mylistAction($arg)
	{
		base::init('member')->needLogin();
		
		$userinfo	= base::init('member')->getInfo();
		$template	= $this->template();
		
		//常见问题
		$column		= sort_combine(base::init('column')->fetchAll('classid=525','','','',false),'classid',true);
		$faq		= base::init('article')->fetchAll('classid=525');
		$faq		= base::init('article')->getTempList($faq,$column);
		
		
		$list		= base::init('classified')->fetchAll(array('writer'=>$userinfo['username']));
		$classids	= array_get_unique($list,'classid');
		$columns	= sort_combine(base::init('column')->fetchAll(array('classid'=>$classids),'','','',false),'classid',true);
		$list		= base::init('classified')->getTempList($list, $columns);
				
		$template->assign('faq', $faq);
		$template->assign('list', $list);
		$template->assign('userinfo', $userinfo);
		$template->display('member_mylist.php');
	}
	
	//企业信息
	public function businessAction($arg)
	{
		base::init('member')->needLogin();
		$userinfo	= base::init('member')->getInfo();
		$template	= $this->template();
		
		if (isPost())
		{
			base::init('business')->editMyBusiness($arg);
			$template->assign('referrer','/app/?m=member&a=home'); 
			$template->assign('stat','修改成功');
			$template->display('refresh.php');
			die;
		}
		
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
		
		//常见问题
		$column		= sort_combine(base::init('column')->fetchAll('classid=525','','','',false),'classid',true);
		$faq		= base::init('article')->fetchAll('classid=525');
		$faq		= base::init('article')->getTempList($faq,$column);
		$list		= base::init('classified')->fetchAll(array('writer'=>$userinfo['username']));
		$business	= base::init('business')->fetchRowByUid($userinfo['id']);
		
		$template->assign('business', $business);
		$template->assign('columns', $columns);
		$template->assign('faq', $faq);
		$template->assign('list', $list);
		$template->assign('userinfo', $userinfo);
		$template->display('member_business.php');
	}
}